

import {
  submitReply,
  queryReplyPorductInfo,
  getOrderCommentSku
} from "../../../../service/jb_common_service"
import {
  chooseMultipleOSSImgUploadQiniuAction
} from "../../../../service/upload_service"
import {
  getFrontUploadSign
} from "../../../../service/config"
import debounce from "../../../../tools/debounce"
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {

    requestData: {
      orderNo: "",
      productId: null,
      productScore: 5,//商品分数
      serviceScore: 5,//服务分数
      comment: "",//评价内容
      pics: "",//评论图片
      avatar: "",//评论人头像
      nickname: '',
      sku: null,
      unique: null,
    },
    uniId: null,//应该是attrId，套餐id
    orderId: null,
    productScores: [5],
    serviceScores: [5],
    reasonImgs: [],//凭证图片


    orderInfo: {

      "cartNum": 1,
      "truePrice": "0.00",
      "storeName": "",
      "image": "",
      "productId": 0,
      "sku": "默认"

    },//订单信息
    productId: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.orderId) {
      this.setData({
        orderId: parseInt(options.orderId),
        uniId: parseInt(options.uni),
        'requestData.unique': options.uni,
        'requestData.orderNo': options.orderNo,
        productId: options.productId,
      })
    }
    // this.getFrontUploadSign();
    this.queryReplyPorductInfo();
    this.getOrderCommentSku();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  queryReplyPorductInfo() {
    wx.showLoading();
    queryReplyPorductInfo({ orderId: this.data.orderId, uni: this.data.uniId }).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        this.setData({
          orderInfo: res.data,
          'requestData.productId': res.data.productId,
          'requestData.sku': res.data.sku,
        })
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '查询错误',
        icon: "none"
      })
      console.error("JJLog:queryReplyPorductInfo", err);
    })
  },
  getOrderCommentSku() {
    getOrderCommentSku(this.data.orderId, this.data.productId, this.data.uniId).then(res => {
      if (res.code == 200) {
        this.setData({
          'requestData.productScore': res.data.productScore || 5,
          'requestData.serviceScore': res.data.serviceScore || 5,
          'requestData.comment': res.data.comment || '默认好评',
        })
        // let pics = 'https://images.jiabao19.com/images/product/2024-02-05/1707103472173/f51e6004e7a443246325bff89b97aae.jpg,https://images.jiabao19.com/images/product/2024-02-05/1707103472173/f51e6004e7a443246325bff89b97aae.jpg,https://images.jiabao19.com/images/product/2024-02-05/1707103472173/f51e6004e7a443246325bff89b97aae.jpg'
        if (res.data.pics) {
          let arr = res.data.pics.split(',');
          let tempArr = [];
          arr.forEach(i => {
            tempArr.push({ imgUrl: i })
          })
          this.setData({
            reasonImgs: tempArr
          })

        }

      }
    }).catch(err => {

    })
  },

  previewImgs(e) {
    let url = e.currentTarget.dataset.img;
    let urls = this.data.reasonImgs.map(i => i.imgUrl);
    wx.previewImage({
      urls: urls,
      current: url
    })
  },

  // 提交退款申请
  submitReplyAction() {
    if (!this.data.requestData.comment.trim()) {
      wx.showToast({
        title: '请填写评价内容',
        icon: "none"
      })
      return;
    }
    let pics = []
    if (this.data.reasonImgs && this.data.reasonImgs.length > 0) {
      pics = this.data.reasonImgs.map(i => i.imgUrl);
    }
    if (pics.length > 0) {
      this.setData({
        'requestData.pics': pics.join(',')
      })
    }
    console.log("JJLog:评价参数:" + JSON.stringify(this.data.requestData));

    debounce(this.submitReplyRequest)
  },
  submitReplyRequest() {
    wx.showLoading();
    submitReply(this.data.requestData).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.redirectTo({
          url: '/featurePackage/pages/commonSuccess/commonSuccess?type=2',

        })
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '评价失败',
        icon: "none"
      })
      console.error("JJLog:submitReply", err);
    })
  },




  bindRemarkText(e) {
    this.setData({
      'requestData.comment': e.detail.value
    })
  },

  // 商品评价分数变化
  productScoreChange(e) {
    this.setData({
      'requestData.productScore': e.detail.value,
    })
  },
  serviceScoreChange(e) {
    this.setData({
      'requestData.serviceScore': e.detail.value,
    })
  },
  //上传图片方法，多张上传哦。
  pushReplyImgAction() {
    let num = 3;
    let imgLength = this.data.reasonImgs.length;
    let arr = this.data.reasonImgs || []
    chooseMultipleOSSImgUploadQiniuAction("reply", num - imgLength).then(res => {
      console.log("JJLog:多张图片上传:" + JSON.stringify(res));
      if (res && res.length > 0) {
        res.forEach(i => {
          arr.push(i)
        })
        this.setData({
          reasonImgs: arr
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },
  cancelOneImg(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.reasonImgs;
    arr.splice(index, 1);
    this.setData({
      reasonImgs: arr
    })
  },

  getFrontUploadSign() {
    getFrontUploadSign().then(res => {
      console.log("JJLog:获取到的图片签名是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.setStorageSync('ossHost', res.data.host);
        wx.setStorageSync('ossSignature', res.data.signature);
        wx.setStorageSync('ossPolicy', res.data.policy);
        wx.setStorageSync('ossAccessId', res.data.accessId);
        wx.setStorageSync('ossCallBack', res.data.callback);
        wx.setStorageSync('ossDir', res.data.dir);
      }
    }).catch(err => {
      console.error("JJLog:getFrontUploadSign", err);
    })
  },

})